package com.supinfo.youFood;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import com.supinfo.food.define.Define;
import com.supinfo.food.entities.TableRestaurant;
import com.supinfo.youFood.obj.VarGlobal;
import com.supinfo.youFood.screen.SerialisationTools;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Room activity
 * @author Keitaro
 *
 */
public class RoomActivity extends Activity {

	private VarGlobal GLOBAL = VarGlobal.getInstance();
	private ListView mList;
	private SimpleAdapter mAdapter;
	private List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.room);
		this.setTitle("Choose your table");

		// Set list
		mList = (ListView) findViewById(R.id.rooms);
		mAdapter = new SimpleAdapter(this, list, R.layout.listidtext, new String[]{"id", "text"}, new int[]{R.id.listIdText_id, R.id.listIdText_text});
		mList.setAdapter(mAdapter);
		mList.setOnItemClickListener(new OnClick());
		
		// Launch task
		RoomWSAndroid restaurantTask = new RoomWSAndroid();
		restaurantTask.execute((Long) GLOBAL.getVarList().get("restaurant"));
	}
	
	/**
	 * Call WS for restaurants list
	 * @author Keitaro
	 *
	 */
	private class RoomWSAndroid extends AsyncTask<Long, Void, SoapObject> {

		private static final String NAMESPACE = "android.services.food.supinfo.com";
		private static final String URL = Define.IP + "/services/RestaurantWSAndroid?wsdl";
		private static final String SOAP_ACTION = "RestaurantWSAndroid";
		private static final String METHOD_ACTION = "getRoom";
		private ProgressDialog dialog;
		
		@Override
		protected void onPreExecute() {
			dialog = ProgressDialog.show(RoomActivity.this, "Loading", "Please wait...", true);
		}
		
		@Override
		protected SoapObject doInBackground(Long... params) {
			SoapObject reponse = null;
			try
		    {
		    	SoapObject request = new SoapObject(NAMESPACE, METHOD_ACTION);
		    	request.addProperty("id", params[0]);
		    	SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		    	envelop.setOutputSoapObject(request);
		    	AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
		    	androidHttpTransport.call(SOAP_ACTION, envelop);
		    	reponse = (SoapObject) envelop.bodyIn;
		    } catch (Exception e)
		    {
		    	Log.e("getRoom", "", e);
		    }
			return reponse;
		}
		
		@Override
		protected void onPostExecute(SoapObject result) {
			List<TableRestaurant> resultList = (List<TableRestaurant>) SerialisationTools.deserialisation(result.getProperty(0).toString());
			if (resultList == null)
				return ;
			
			// Create list
			list.clear();
			for (TableRestaurant table : resultList) {
				Log.i("Data Table Restaurant", table.getId() + " => " + table.getNumero());
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("id", table.getId().toString());
	            map.put("text", table.getNumero().toString());
	            list.add(map);
			}
			mAdapter.notifyDataSetChanged();
			
			// Dialog
			dialog.dismiss();
		}
	}
	
	/**
	 * Action on item
	 * @author Keitaro
	 *
	 */
	private class OnClick implements OnItemClickListener {

		public void onItemClick(AdapterView<?> a, View v, int position, long id) {
			HashMap<String, String> map = (HashMap<String, String>) mList.getItemAtPosition(position);
			GLOBAL.getVarList().put("room", Long.valueOf(map.get("id")));
			GLOBAL.getVarList().put("roomNum", map.get("text").toString());
			startActivity(new Intent(RoomActivity.this, YouFoodActivity.class));
		}
		
	}
	
}
